********************************************************************************
*Figure B12; Panel A and C 
********************************************************************************

clear all
set matsize 10000
set maxvar 40000
set more off
global data " "
global dir " "


preserve

forv i=2(1)1001{

clear
use "placebo_treatment.dta"

cap drop provincerandomid
cap drop treatmentyear

gen provincerandomid=uniform()
sort provincerandomid

gen treatmentyear=.
replace treatmentyear=1966 in 1
replace treatmentyear=1967 in 2/5
replace treatmentyear=1968 in 6
replace treatmentyear=1969 in 7/16
replace treatmentyear=1970 in 17/27
replace treatmentyear=1971 in 28/40



drop provincerandomid
sort mom_birth_state
save "$data/placebo_treatment`i'", replace

}
restore

 
preserve
clear all
 use "$data/estimation_sample_blacks"
tempname p1
qui{
postfile `p1' float rep t_1 t_2 using "$data/placebo1.dta" , replace

local c=0
foreach X in newbw2500 newpretermLT37  { // outcomes set 1
local c=`c'+1

global treat_var "plus0 plus1 plus2 plus3 plus4"
global state_var "Exitexam Perstudentexpenditure Studentteacherratio Povertyrate Unemploymentrate"
foreach x in $treat_var{
sum `x', meanonly
scalar mean`x'=r(mean)



}
 
reg `X' plus0 plus1 plus2 plus3 plus4 i.by i.age_cat  i.mom_birth_state i.birth_month i.year  i.tbo i.male   Exitexam Perstudentexpenditure Studentteacherratio Povertyrate Unemploymentrate i.division#c.by  , cluster(mom_birth_state)

local V=meanplus0+meanplus1+meanplus2+meanplus3+meanplus4
local beta`c'=(_b[plus0]*meanplus0/`V')+(_b[plus1]*meanplus1/`V')+(_b[plus2]*meanplus2/`V')+(_b[plus3]*meanplus3/`V')+(_b[plus4]*meanplus4/`V')

}

post `p1' (1) (`=`beta1'') (`=`beta2'')   ///


  }  
  
  
  
  
forv i=2(1)1001 {
disp `i'
qui{
cap drop _merge
cap drop treatmentyear

cap drop plus0
cap drop plus1
cap drop plus2
cap drop plus3 
cap drop plus4
sort mom_birth_state
merge mom_birth_state using "$data/placebo_treatment`i'", uniqusing


cap drop index
gen index= by-treatmentyear
gen plus0=0
replace plus0=1 if index==0
gen plus1=0
replace plus1=1 if index==1
gen plus2=0
replace plus2=1 if index==2
gen plus3=0
replace plus3=1 if index==3
gen plus4=0
replace plus4=1 if index>=4 
local c=0

global treat_var "plus0 plus1 plus2 plus3 plus4"
global state_var "Exitexam Perstudentexpenditure Studentteacherratio Povertyrate Unemploymentrate"
foreach x in $treat_var{
sum `x', meanonly
scalar mean`x'=r(mean)
}
 
 
foreach X in  newbw2500 newpretermLT37   {
        local c=`c'+1
		    
reg `X' plus0 plus1 plus2 plus3 plus4 i.by i.age_cat  i.mom_birth_state i.birth_month i.year    i.tbo i.male Exitexam Perstudentexpenditure Studentteacherratio Povertyrate Unemploymentrate i.division#c.by  , cluster(mom_birth_state)
local V=meanplus0+meanplus1+meanplus2+meanplus3+meanplus4
local beta`c'=(_b[plus0]*meanplus0/`V')+(_b[plus1]*meanplus1/`V')+(_b[plus2]*meanplus2/`V')+(_b[plus3]*meanplus3/`V')+(_b[plus4]*meanplus4/`V')

            }
		
post `p1' (`i') (`=`beta1'') (`=`beta2'')   ///

}
}
postclose `p1' 

clear

********************************************************************************


clear all
use "$data/placebo1.dta" 
	
summ t_1 in 1
	local t_1 = r(mean)
	count if t_1>`t_1'
	count if abs(t_1)>abs(`t_1')
	histogram t_1, scheme(s1color) xline(`t_1', extend) xtitle("Beta (BW2500)") ///
		text(1 -0.2 "p=`=`r(N)'/1000'", place(e)) saving(1_blacks_updated_dpi, replace) xscale(range(-0.6(0.2)0.6)) 
	
kdensity t_1,  kernel(gau) c(direct) lc(blue) lw(medium)  xline(-.35854471, lcolor(red) lstyle(grid)) legend(off) graphregion(fcolor(white)) xlabel (-0.6(0.2)0.6, nogrid) ylabel(, nogrid) ///
xtitle("Placebo Estimates-Low Birth Weight" "Prob(placebo<actual)=0.005") ytitle("Frequency") title("", nobox) note("")
graph save "figure_b12_panela.gph", replace


	
	
summ t_2 in 1
	local t_2 = r(mean)
	count if t_2>`t_2'
	count if abs(t_2)>abs(`t_2')
	histogram t_2, scheme(s1color) xline(`t_2') xtitle("Beta (Premature)") ///
		text(2 -0.1 "p=`=`r(N)'/1000'", place(e)) saving(2_blacks_updated_dpi, replace) xscale(range(-0.6(0.2) 0.6)) 	

kdensity t_2,  kernel(gau) c(direct) lc(blue) lw(medium)  xline(-.528368, lcolor(red) lstyle(grid)) legend(off) graphregion(fcolor(white)) xlabel (-0.6(0.2) 0.6, nogrid) ylabel(, nogrid) ///
xtitle("Placebo Estimates-Prematurity" "Prob(placebo<actual)=0.001") ytitle("Frequency") title("", nobox) note("")
graph save "figure_b12_panelc.gph", replace		
	
	

********************************************************************************
*Figure B12; Panel B and D 
********************************************************************************

clear all
set matsize 10000
set maxvar 40000
set more off
global data " "
global dir " "


preserve

forv i=2(1)1001{

clear
use "placebo_treatment.dta"

cap drop provincerandomid
cap drop treatmentyear

gen provincerandomid=uniform()
sort provincerandomid

gen treatmentyear=.
replace treatmentyear=1966 in 1
replace treatmentyear=1967 in 2/5
replace treatmentyear=1968 in 6
replace treatmentyear=1969 in 7/16
replace treatmentyear=1970 in 17/27
replace treatmentyear=1971 in 28/40



drop provincerandomid
sort mom_birth_state
save "$data/placebo_treatment`i'", replace

}
restore

 
preserve
clear all
 use "$data/estimation_sample_whites"
tempname p1
qui{
postfile `p1' float rep t_1 t_2 using "$data/placebo1.dta" , replace

local c=0
foreach X in newbw2500 newpretermLT37  { // outcomes set 1
local c=`c'+1

global treat_var "plus0 plus1 plus2 plus3 plus4"
global state_var "Exitexam Perstudentexpenditure Studentteacherratio Povertyrate Unemploymentrate"
foreach x in $treat_var{
sum `x', meanonly
scalar mean`x'=r(mean)



}
 
reg `X' plus0 plus1 plus2 plus3 plus4 i.by i.age_cat  i.mom_birth_state i.birth_month i.year  i.tbo i.male   Exitexam Perstudentexpenditure Studentteacherratio Povertyrate Unemploymentrate i.division#c.by  , cluster(mom_birth_state)

local V=meanplus0+meanplus1+meanplus2+meanplus3+meanplus4
local beta`c'=(_b[plus0]*meanplus0/`V')+(_b[plus1]*meanplus1/`V')+(_b[plus2]*meanplus2/`V')+(_b[plus3]*meanplus3/`V')+(_b[plus4]*meanplus4/`V')

}

post `p1' (1) (`=`beta1'') (`=`beta2'')   ///


  }  
  
  
  
  
forv i=2(1)1001 {
disp `i'
qui{
cap drop _merge
cap drop treatmentyear

cap drop plus0
cap drop plus1
cap drop plus2
cap drop plus3 
cap drop plus4
sort mom_birth_state
merge mom_birth_state using "$data/placebo_treatment`i'", uniqusing


cap drop index
gen index= by-treatmentyear
gen plus0=0
replace plus0=1 if index==0
gen plus1=0
replace plus1=1 if index==1
gen plus2=0
replace plus2=1 if index==2
gen plus3=0
replace plus3=1 if index==3
gen plus4=0
replace plus4=1 if index>=4 
local c=0

global treat_var "plus0 plus1 plus2 plus3 plus4"
global state_var "Exitexam Perstudentexpenditure Studentteacherratio Povertyrate Unemploymentrate"
foreach x in $treat_var{
sum `x', meanonly
scalar mean`x'=r(mean)
}
 
 
foreach X in  newbw2500 newpretermLT37   {
        local c=`c'+1
		    
reg `X' plus0 plus1 plus2 plus3 plus4 i.by i.age_cat  i.mom_birth_state i.birth_month i.year    i.tbo i.male Exitexam Perstudentexpenditure Studentteacherratio Povertyrate Unemploymentrate i.division#c.by  , cluster(mom_birth_state)
local V=meanplus0+meanplus1+meanplus2+meanplus3+meanplus4
local beta`c'=(_b[plus0]*meanplus0/`V')+(_b[plus1]*meanplus1/`V')+(_b[plus2]*meanplus2/`V')+(_b[plus3]*meanplus3/`V')+(_b[plus4]*meanplus4/`V')

            }
		
post `p1' (`i') (`=`beta1'') (`=`beta2'')   ///

}
}
postclose `p1' 

clear

********************************************************************************


clear all
use "$data/placebo1.dta" 
	
	
summ t_1 in 1
	local t_1 = r(mean)
	count if t_1>`t_1'
	count if abs(t_1)>abs(`t_1')
	histogram t_1, scheme(s1color) xline(`t_1', extend) xtitle("Beta (BW2500)") ///
		text(1 -0.2 "p=`=`r(N)'/1000'", place(e)) saving(1_whites_updated_dpi, replace) xscale(range(-0.15(0.05)0.15)) 
	
kdensity t_1,  kernel(gau) c(direct) lc(blue) lw(medium)  xline(-.05279796, lcolor(red) lstyle(grid)) legend(off) graphregion(fcolor(white)) xlabel (-0.15(0.05)0.15, nogrid) ylabel(, nogrid) ///
xtitle("Placebo Estimates-Low Birth Weight" "Prob(placebo<actual)=0.171") ytitle("Frequency") title("", nobox) note("")
graph save "figure_b12_panelb.gph", replace


	
	
summ t_2 in 1
	local t_2 = r(mean)
	count if t_2>`t_2'
	count if abs(t_2)>abs(`t_2')
	histogram t_2, scheme(s1color) xline(`t_2') xtitle("Beta (Premature)") ///
		text(2 -0.1 "p=`=`r(N)'/1000'", place(e)) saving(2_whites_updated_dpi, replace) xscale(range(-0.3(0.1) 0.3)) 	

kdensity t_2,  kernel(gau) c(direct) lc(blue) lw(medium)  xline(-.06879538, lcolor(red) lstyle(grid)) legend(off) graphregion(fcolor(white)) xlabel (-0.3(0.10) 0.3, nogrid) ylabel(, nogrid) ///
xtitle("Placebo Estimates-Prematurity" "Prob(placebo<actual)=0.222") ytitle("Frequency") title("", nobox) note("")
graph save "figure_b12_paneld.gph", replace		
	
	



